grammar secondgrammar;

tokens {
PLUS 	= '+' ;
MINUS	= '-' ;
MULT	= '*' ;
DIV	= '/' ;
RPAREN	= ')' ;
LPAREN	= '(' ;
ASSIGN	= '=' ;
SEMICOLON	= ';';
VAR	= 'var';
UNITS	= 'unidades';
COMMA	= ',';
POWER	= '^';
}

ID  :	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

INT :	('-'|'+')?'0'..'9'+
    ;

FLOAT
    :   ('-'|'+')?('0'..'9')+ '.' ('0'..'9')* EXPONENT?
    |   ('-'|'+')?'.' ('0'..'9')+ EXPONENT?
    |   ('-'|'+')?('0'..'9')+ EXPONENT
    ;

COMMENT
    :   '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    |   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;


/*----------------
* PARSER RULES
*----------------*/

prog :		stat+;
stat :		UNITS ids SEMICOLON
	|	expr SEMICOLON
	|	VAR ID ASSIGN expr SEMICOLON
	|	SEMICOLON; 

ids	:	ID (COMMA ID)*;

expr 	:	multExpr ((PLUS | MINUS )multExpr)*;

multExpr	:	atom ((MULT | DIV|) atom )*;

atom 	:	INT ID(POWER INT)?
	|	FLOAT ID(POWER INT)?
	|	ID(POWER INT)?
	|	LPAREN expr RPAREN;

fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

